#ecoding=utf-8
import xlwings as xw
import win32api


class BridgeBumpReport:
    def __init__(self):
        app = xw.App(visible=True, add_book=False)
        # self.wb = app.books.open(r'F:\公司\宁波报表\桥头跳车.xls')
        # self.sht = self.wb.sheets['sheet1']
        self.wb = app.books.add()
        self.sht = self.wb.sheets['sheet1']
        self.create_sheet(self.sht)

    def save_wb(self,path):
        _wb = self.wb
        _wb.save(path)
        _wb.close()

    def write_info(self, info):
        sht = self.sht
        name = sht.range('B2')
        direction = sht.range('D2')
        date = sht.range('F2')
        v_dtpitch = sht.range('B3')
        stride = sht.range('D3')
        dt_dis = sht.range('F3')

        up_lon = sht.range('C5')
        up_lat = sht.range('C6')
        up_interval = sht.range('C7')
        up_num_total = sht.range('C8')
        up_num_bump = sht.range('C9')
        up_per_bump = sht.range('C10')
        up_bump_arr = sht.range('A12')

        down_lon = sht.range('F5')
        down_lat = sht.range('F6')
        down_interval = sht.range('F7')
        down_num_total = sht.range('F8')
        down_num_bump = sht.range('F9')
        down_per_bump = sht.range('F10')
        down_bump_arr = sht.range('D12')

        name.value = info.name
        direction.value = info.direction
        date.value = info.date
        v_dtpitch.value = info.v_dtpitch
        stride.value = info.stride
        dt_dis.value = info.dt_dis

        up_lon.value = info.up_info.lon
        up_lat.value = info.up_info.lat
        up_interval.value = info.up_info.interval
        up_num_total.value = info.up_info.num_total
        up_num_bump.value = info.up_info.num_bump
        up_per_bump.value = info.up_info.per_bump
        up_bump_arr.value = info.up_info.bump_arr

        down_lon.value = info.down_info.lon
        down_lat.value = info.down_info.lat
        down_interval.value = info.down_info.interval
        down_num_total.value = info.down_info.num_total
        down_num_bump.value = info.down_info.num_bump
        down_per_bump.value = info.down_info.per_bump
        down_bump_arr.value = info.down_info.bump_arr

        num_extend = max(len(info.down_info.bump_arr),len(info.up_info.bump_arr))
        for i in range(num_extend):
            for j in range(1, 7):
                content = sht.range(12+i, j)
                content.row_height = 20  # 设置第1行 行高
                self.set_style(content, 2)
                for bd_id in range(7, 13):
                    content.api.Borders(bd_id).LineStyle = 1
                    content.api.Borders(bd_id).Weight = 2

    def create_sheet(self,sht):
        for i in range(1, 12):
            sht.range(i, 1).row_height = 24  # 设置第4列 列宽。（1,4）为第1行第4列的单元格
            for j in range(1, 7):
                for bd_id in range(7, 13):
                    sht.range(i, j).api.Borders(bd_id).LineStyle = 1
                    sht.range(i, j).api.Borders(bd_id).Weight = 2
        for j in range(1, 7):
            sht.range(1, j).column_width = 15  # 设置第1行 行高
        sht.range('A1:F1').api.merge
        sht.range('A4:C4').api.merge
        sht.range('D4:F4').api.merge
        sht.range('A5:A6').api.merge
        sht.range('D5:D6').api.merge
        sht.range('A7:B7').api.merge
        sht.range('A8:B8').api.merge
        sht.range('A9:B9').api.merge
        sht.range('A10:B10').api.merge
        sht.range('D7:E7').api.merge
        sht.range('D8:E8').api.merge
        sht.range('D9:E9').api.merge
        sht.range('D10:E10').api.merge
        sht.range('A1').value = '桥头跳车检测报告'
        self.set_style(sht.range('A1'),4)
        sht.range('A2').value = '桥名'
        self.set_style(sht.range('A2'), 1)
        sht.range('A3').value = '坡差阈值(%)'
        self.set_style(sht.range('A3'), 1)
        sht.range('A4').value = '上桥桥段'
        self.set_style(sht.range('A4'), 3)
        sht.range('A5').value = '桥接缝GPS信息'
        self.set_style(sht.range('A5'), 1)
        sht.range('A7').value = '单侧区间长度(m)'
        self.set_style(sht.range('A7'), 1)
        sht.range('A8').value = '取点总数'
        self.set_style(sht.range('A8'), 1)
        sht.range('A9').value = '跳车点数'
        self.set_style(sht.range('A9'), 1)
        sht.range('A10').value = '跳车比例(%)'
        self.set_style(sht.range('A10'), 1)
        sht.range('A11').value = '序号'
        self.set_style(sht.range('A11'), 1)
        self.set_style(sht.range('B2'), 2)
        self.set_style(sht.range('B3'), 2)
        sht.range('B5').value = '经度'
        self.set_style(sht.range('B5'), 1)
        sht.range('B6').value = '纬度'
        self.set_style(sht.range('B6'), 1)
        sht.range('B11').value = '相对距离(m)'
        self.set_style(sht.range('B11'), 1)
        sht.range('C2').value = '方向'
        self.set_style(sht.range('C2'), 1)
        sht.range('C3').value = '判定步长(m)'
        self.set_style(sht.range('C3'), 1)
        self.set_style(sht.range('C5'), 2)
        self.set_style(sht.range('C6'), 2)
        self.set_style(sht.range('C7'), 2)
        self.set_style(sht.range('C8'), 2)
        self.set_style(sht.range('C9'), 2)
        self.set_style(sht.range('C10'), 2)
        sht.range('C11').value = '坡差(‰)'
        self.set_style(sht.range('C11'), 1)
        self.set_style(sht.range('D2'), 2)
        self.set_style(sht.range('D3'), 2)
        sht.range('D4').value = '下桥桥段'
        self.set_style(sht.range('D4'), 3)
        sht.range('D5').value = '桥接缝GPS信息'
        self.set_style(sht.range('D5'), 1)
        sht.range('D7').value = '单侧区间长度(m)'
        self.set_style(sht.range('D7'), 1)
        sht.range('D8').value = '取点总数'
        self.set_style(sht.range('D8'), 1)
        sht.range('D9').value = '跳车点数'
        self.set_style(sht.range('D9'), 1)
        sht.range('D10').value = '跳车比例(%)'
        self.set_style(sht.range('D10'), 1)
        sht.range('D11').value = '序号'
        self.set_style(sht.range('D11'), 1)
        sht.range('E2').value = '检测日期'
        self.set_style(sht.range('E2'), 1)
        sht.range('E3').value = '判定间距(m)'
        self.set_style(sht.range('E3'), 1)
        sht.range('E5').value = '经度'
        self.set_style(sht.range('E5'), 1)
        sht.range('E6').value = '纬度'
        self.set_style(sht.range('E6'), 1)
        sht.range('E11').value = '相对距离(m)'
        self.set_style(sht.range('E11'), 1)
        self.set_style(sht.range('F2'), 2)
        self.set_style(sht.range('F3'), 2)
        self.set_style(sht.range('F5'), 2)
        self.set_style(sht.range('F6'), 2)
        self.set_style(sht.range('F7'), 2)
        self.set_style(sht.range('F8'), 2)
        self.set_style(sht.range('F9'), 2)
        self.set_style(sht.range('F10'), 2)
        sht.range('F11').value = '坡差(‰)'
        self.set_style(sht.range('F11'), 1)

    def set_style(self, x, num):
        if num == 1:
            style = ['宋体', 12, True, -4108]  # Font,size,Bold,HorizontalAlignment
        elif num == 2:
            style = ['宋体', 12, False, -4108]  # Font,size,Bold,HorizontalAlignment
        elif num == 3:
            style = ['宋体', 15, True, -4108]  # Font,size,Bold,HorizontalAlignment
        elif num == 4:
            style = ['宋体', 20, True, -4108]  # Font,size,Bold,HorizontalAlignment
        for i in range(len(style)):
            x.api.Font.Name = style[0]
            x.api.Font.Size = style[1]
            x.api.Font.Bold = style[2]
            x.api.HorizontalAlignment = style[3]







class BridgeBumpInfo:
    def __init__(self):
        self.name = ''
        self.direction = ''
        self.date = ''
        self.v_dtpitch = -1
        self.stride = -1
        self.dt_dis = -1
        self.up_info = BridgeBumpDetail()
        self.down_info = BridgeBumpDetail()


class BridgeBumpDetail:
    def __init__(self):
        self.interval = -1
        self.lon = ''
        self.lat = ''
        self.num_total = -1
        self.num_bump = 0
        self.bump_arr = None
        self.per_bump = 0


if __name__ == '__main__':
    report = BridgeBumpReport()
    # report.save_wb(r'J:\code\报表\test.xlsx')

